<?php
class MainController extends AppController{

	 var $name = 'Main';

	 var $components = array('Site');
	 var $uses=null;

	 function index() {
	 	$this->useModel('User',$this);

		$fields=array('User.login','User.name','User.vote');
		$this->set('userstop', $this->User->findAll("User.type>0 AND User.comcount>0",$fields,"User.vote DESC, User.comcount DESC",10,null,0));

		$fields=array('Post.id','Post.title','Post.replies','Post.modified','Post.points','Post.rated','Category.name','Category.description','User.login','User.name');

		$this->set('poststop', $this->User->Post->findAll(null,$fields,"Post.created DESC",10,null,0));

		$fields=array('Post.id','Post.title','Post.replies','Post.created','Post.content','Category.name','Category.description','User.login','User.name');
		$this->set('postlog', $this->User->Post->find("Post.category_id=6 AND Post.created>DATE_SUB(NOW(),INTERVAL 20 DAY)",$fields,"Post.created DESC",0));

    $fields=array('Comment.id','Comment.text','Comment.created','Post.id','Post.title','User.login','User.name');
		$this->set('commentslast', $this->User->Comment->findAll("Comment.del = 0",$fields,"Comment.created DESC",10,null,0));

		$this->set('tagstop', $this->User->Post->Tag->topTags(10));
    $this->Site->getCalendar();
	 }

	 function search() {
	 	$type=null; $query=null;
		if(empty($this->params['url']['t']))
			$type=1;
		else
    	$type=$this->params['url']['t'];

			$this->set('type', $type);

   if(empty($this->params['url']['q'])) {
			$this->render();
			return;
		}

		$query=trim($this->params['url']['q']);
		$this->set('query', $query);

		mb_internal_encoding("UTF-8");

		$words=explode(" ",$query);
        foreach($words as $word) {
        	if(mb_strlen($word)<3) {
					$this->set('error', 1);
					$this->render();
					return;
			}
		}

        switch ($type) {
			case 3:
				$this->useModel('User',$this);
				$this->User->recursive = 0;
				$q="User.login LIKE '%".strtolower($query)."%' AND User.type>0";
				$fields=array('User.login','User.name','User.vote','User.postcount','User.comcount','User.created');
				$count=$this->User->findCount($q);
				$users=$this->User->findAll($q,$fields,"User.login ASC",PAGESIZE,$this->pager($count,PAGESIZE),0);
				if(empty($users))
					$this->set('no', 1);
				else
					$this->set('users', $users);
				$this->set('count', $count);
				break;
			case 2:
				$this->useModel('Comment',$this);
      	$q="1=1";
				foreach($words as $word) {
					$q.=" AND Comment.text LIKE '%{$word}%'";
				}
        $count=$this->Comment->findCount($q);
        $fields=array('Comment.id','Comment.text','Comment.vote','Comment.created','Post.id','Post.title','User.login','User.name');
				$comments=$this->Comment->findAll($q,$fields,"Comment.created DESC",PAGESIZE,$this->pager($count,PAGESIZE),0);
        if(empty($comments))
					$this->set('no', 1);
				else
					$this->set('comments', $comments);
					$this->set('count', $count);
			   break;
			case 1:
			default:
				$this->useModel('Post',$this);
        $q="1=1";
				foreach($words as $word) {
					$q.=" AND Post.title LIKE '%{$word}%'";
				}
        $count=$this->Post->findCount($q);
        $fields=array('Post.id','Post.title','Post.replies','Post.closed','Post.created','Post.points','Post.rated','Post.modified','Category.name','Category.description','User.login','User.name');
				$posts=$this->Post->findAll($q,$fields,"Post.modified DESC",PAGESIZE,$this->pager($count,PAGESIZE),0);
        if(empty($posts))
					$this->set('no', 1);
				else
					$this->set('posts', $posts);
					$this->set('count', $count);
			   break;
			}

	 }

	 function admin_index () {
	 	$this->checkAdmin(); $this->layout='admin';
		$this->useModel(array('Post','Comment','User','Tag'),$this);
        $fields=array('User.id','User.login','User.name','User.type','User.comcount','User.postcount','User.vote');
		$this->set('users', $this->User->findAll(null,$fields,"User.created DESC",10,null,0));
        $fields=array('Post.id','Post.title','Post.replies','Post.created','Category.name','Category.description','User.login','User.name');
		$this->set('posts', $this->Post->findAll(null,$fields,"Post.created DESC",10,null,0));
        $fields=array('Comment.id','Comment.text','Comment.created','Post.id','Post.title','User.login','User.name');
		$this->set('comments', $this->Comment->findAll(null,$fields,"Comment.created DESC",10,null,0));
		$this->set('tags', $this->Tag->findAll(null,null,"Tag.id DESC",10,null,0));
	 }

}
?>